//
//  UserCenterSelect.m
//  cutter
//
//  Created by mac on 16/1/5.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "UserCenterNew.h"
#import "UserCenterList.h"
#import "UserCenterPick.h"
@interface UserCenterPickCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UIButton *selectedView;
@property(nonatomic, weak) IBOutlet UITextField *nameView;
@end
@implementation UserCenterPickCell
-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
    [self.selectedView setHidden:!selected];
}
@end
@implementation UserCenterPick
-(void)viewDidLoad{
    [super viewDidLoad];
    [self setSetCurrent:YES];
    [self setClearsSelectionOnViewWillAppear:NO];
    //
    if (self.client == nil) {
        if ([TMGlobal valueForKey:@"client"]) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }else{
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //
    UIView *parent = [self.view superview];
    if (parent) {
        [parent.layer setCornerRadius:6];
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([TMGlobal valueForKey:@"client"]) {
        return 3;
    }
    return 2;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if ([TMGlobal valueForKey:@"client"]) {
        if (section == 1) {
            return @"当前客户";
        }
        if (section == 2) {
            return @"已有客户";
        }
    }else{
        if (section == 1) {
            return @"已有客户";
        }
    }
    return nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UserCenterPickCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nullIdentifier"];
        return cell;
    }
    if (indexPath.section == 1) {
        id client = [TMGlobal valueForKey:@"client"];
        if (client) {
            UserCenterPickCell *cell = [tableView dequeueReusableCellWithIdentifier:@"currIdentifier"];
            [cell.nameView setText:[client objectForKey:@"truename"]];
            return cell;
        }
    }
    UserCenterPickCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if (self.client) {
        [cell.nameView setText:[self.client objectForKey:@"truename"]];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [self setClient:nil];
    }
    if (indexPath.section == 1) {
        id client = [TMGlobal valueForKey:@"client"];
        if (client) {
            [self setClient:client];
        }
    }
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    __weak UserCenterPick *this = self;
    if ([segue.destinationViewController isKindOfClass:UserCenterList.class]) {
        UserCenterList *ctrl = segue.destinationViewController;
        [ctrl setClient:self.client];
        [ctrl setOnSelected:^(id target, id result){
            if (result) {
                [this setClient:result];
                [this.pickName setText:[result objectForKey:@"truename"]];
            }
            [this closeTouch:nil];
        }];
    }
    if ([segue.destinationViewController isKindOfClass:UserCenterNew.class]) {
        UserCenterNew *ctrl = segue.destinationViewController;
        [ctrl setOnSelected:^(id target, id result){
            if (result) {
                [this setClient:result];
                [this.pickName setText:[result objectForKey:@"truename"]];
            }
            [this closeTouch:nil];
        }];
    }
}
-(IBAction)closeTouch:(id)sender {
    if (self.onSelected) {
        if (self.client && self.setCurrent) {
            [TMGlobal setValue:self.client forKey:@"client"];
        }
        self.onSelected(self, self.client);
    }
}
@end


@implementation UserCenterSelect
-(void)viewDidLoad{
    [super viewDidLoad];
    [self setSetCurrent:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //
    if (self.presentationController) {
        UIView *parent = [[self.navigationController view] superview];
        if (parent) {
            [parent.layer setCornerRadius:6];
        }
    }
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    __weak UserCenterSelect *this = self;
    if ([segue.destinationViewController isKindOfClass:UserCenterList.class]) {
        UserCenterList *ctrl = segue.destinationViewController;
        [ctrl setClient:self.client];
        [ctrl setOnSelected:^(id target, id result){
            if (result) {
                [this setClient:result];
                [this closeTouch:nil];
            }else{
                [this.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    if ([segue.destinationViewController isKindOfClass:UserCenterNew.class]) {
        UserCenterNew *ctrl = segue.destinationViewController;
        [ctrl setOnSelected:^(id target, id result){
            if (result) {
                [this setClient:result];
                [this closeTouch:nil];
            }else{
                [this.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
}
-(IBAction)closeTouch:(id)sender {
    if (self.onSelected) {
        if(self.client && self.setCurrent){
            [TMGlobal setValue:self.client forKey:@"client"];
        }
        self.onSelected(self, self.client);
    }
}
@end
